package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private UserDao userDao;
    /**
     * 保存商品报损单
     * @param damageList
     * @param damageListGoodsStr
     * @param damageNumber
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean save(DamageList damageList, String damageListGoodsStr, String damageNumber) {
        //获取用户Id
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        Integer userId = user.getUserId();
        //保存报损单信息
        damageList.setDamageNumber(damageNumber);
        damageList.setUserId(userId);
        damageListGoodsDao.saveDamageList(damageList);
        Gson gosn = new Gson();
        List<DamageListGoods> damageListGoodsList = gosn.fromJson(damageListGoodsStr,new TypeToken<List<DamageListGoods>>(){}.getType());
        /*damageListGoodsList.stream().forEach((damageListGoods)->{
            damageListGoods.setDamageListId(id);
            //保存报损单商品信息
            Boolean flag = damageListGoodsDao.saveDamageListGoods(damageListGoods);
        });*/
        Boolean flag = false;
        for (DamageListGoods damageListGood : damageListGoodsList) {
            damageListGood.setDamageListId(damageList.getDamageListId());
            //保存报损单商品信息
            flag = damageListGoodsDao.saveDamageListGoods(damageListGood);
        }
        if (flag){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询商品报损单列表
     * @param sTime
     * @param eTime
     * @return
     */
    @SneakyThrows
    @Override
    public List<DamageList> selectList(String sTime, String eTime) {
        ArrayList<DamageList> list = new ArrayList<>();
        //获取到输入时间之间的时间集合
        List<String> timeSlotByDay = DateUtil.getTimeSlotByDay(sTime, eTime);
        List<DamageList> damageLists = damageListGoodsDao.selectListForTime();
        for (DamageList damageList : damageLists) {
            if (timeSlotByDay.contains(damageList.getDamageDate())){
                list.add(damageList);
            }
        }
        return list;
    }

    /**
     * 查询报损单下边的所有商品
     * @param damageListId
     * @return
     */
    @Override
    public List<DamageListGoods> getGoodslistFordamageListById(Integer damageListId) {
        List<DamageListGoods> goods = damageListGoodsDao.selectGoodsFordamageListById(damageListId);
        return goods;
    }
}
